package com.tusuo.stds.ui.main;

import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.github.lzyzsd.jsbridge.BridgeHandler;
import com.github.lzyzsd.jsbridge.BridgeWebView;
import com.github.lzyzsd.jsbridge.CallBackFunction;
import com.hannesdorfmann.mosby.mvp.MvpPresenter;
import com.hannesdorfmann.mosby.mvp.MvpView;
import com.tusuo.stds.R;
import com.tusuo.stds.ui.BaseToolbarActivity;
import com.tusuo.stds.ui.ToolbarOwner;
import com.tusuo.stds.util.Utils;
import com.tusuo.stds.util.webview.WebViewManager;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;
import rx.functions.Action0;

/**
 * Created by developer on 17-9-14.
 */

@EActivity(R.layout.activity_general_webview)
public class PageDetailActivity extends BaseToolbarActivity {

    @ViewById(R.id.general_webview_btn_cancel)
    Button btnCancel;

    @ViewById(R.id.general_webview_layout_qzone)
    RelativeLayout layoutQzone;
    @ViewById(R.id.general_webview_layout_moment)
    RelativeLayout layoutMoment;
    @ViewById(R.id.general_webview_layout_wechat)
    RelativeLayout layoutWechat;

    @ViewById(R.id.general_webview_layout_shadow)
    RelativeLayout layoutShadow;
    @ViewById(R.id.general_webview_layout_share)
    RelativeLayout layoutShare;

    @ViewById(R.id.general_webview_wb)
    BridgeWebView mWebView;

    private TranslateAnimation animation_show;
    private TranslateAnimation animation_hide;
    private boolean isShowAnimationFinish = true;
    private boolean isHideAnimationFinish = true;
//    private String mUrl;
    private int mId;

    public static String[] shareNames = {
            "QZone",
            QQ.NAME,
            WechatMoments.NAME,
            Wechat.NAME,
            SinaWeibo.NAME
    };

    private ArrayList<Platform.ShareParams> shareParamsList;
    private String mUrl;
    private String content;
    private String title;
    private String shareUrl;
    private String icon;
    private WebViewManager mWebViewManager;

    @AfterInject
    void init() {

        if (getIntent() != null && getIntent().getExtras() != null) {
            mUrl = getIntent().getStringExtra("url");
//            mId = getIntent().getIntExtra("id", -1);
        }

        initAnimation();
    }

    @AfterViews
    void initData() {
        setupUI(this);
        List<ToolbarOwner.MenuAction> menuActionList = new ArrayList<>();
        ToolbarOwner.MenuAction menuAction = new ToolbarOwner.MenuAction("", new Action0() {
            @Override
            public void call() {
                showShareLayout();
            }
        }, R.drawable.st_found_btn_sharing);

        menuActionList.add(menuAction);

        ToolbarOwner.Config config = new ToolbarOwner.Config("详情", true, menuActionList);
        mToolbarOwner.setConfig(config);

        shareParamsList = new ArrayList<>();

        shareParamsList.add(new QZone.ShareParams());
        shareParamsList.add(new QQ.ShareParams());
        shareParamsList.add(new WechatMoments.ShareParams());
        shareParamsList.add(new Wechat.ShareParams());
        shareParamsList.add(new SinaWeibo.ShareParams());

        mWebViewManager = new WebViewManager(this,mWebView);
        mWebViewManager.init();

        if (!TextUtils.isEmpty(mUrl)) {
            mWebViewManager.loadUrl(mUrl);
            mWebViewManager.registerHandlerShare(shareHandler);
            mWebViewManager.setShareParams();
        }

        if (!Utils.App.isQQAppInstalled(this)) {
            layoutQzone.setVisibility(View.GONE);
        }

        if (!Utils.App.isWXAppInstalled(this)) {
            layoutWechat.setVisibility(View.GONE);
            layoutMoment.setVisibility(View.GONE);
        }
    }

    void initAnimation() {
        animation_show = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0,
                Animation.RELATIVE_TO_PARENT, 0,
                Animation.RELATIVE_TO_PARENT, 1,
                Animation.RELATIVE_TO_PARENT, 0);

        animation_show.setDuration(500);
        animation_show.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                isShowAnimationFinish = false;
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                isShowAnimationFinish = true;
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });


        animation_hide = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0,
                Animation.RELATIVE_TO_PARENT, 0,
                Animation.RELATIVE_TO_PARENT, 0,
                Animation.RELATIVE_TO_PARENT, 1);

        animation_hide.setDuration(500);
        animation_hide.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                isHideAnimationFinish = false;
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                isHideAnimationFinish = true;
                layoutShadow.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

    }

    void showShareLayout() {
        if (layoutShadow.getVisibility() != View.VISIBLE && isShowAnimationFinish && isHideAnimationFinish) {
            layoutShadow.setVisibility(View.VISIBLE);
            layoutShare.startAnimation(animation_show);
        }
    }

    @Click({R.id.general_webview_layout_shadow,R.id.general_webview_btn_cancel})
    void hideShareLayout() {
        if (layoutShadow.getVisibility() == View.VISIBLE && isShowAnimationFinish && isHideAnimationFinish) {
            layoutShare.startAnimation(animation_hide);
        }
    }

    public void setShareParams(String data) {
        try {
            JSONObject json = new JSONObject(data);
            content = json.getString("content");
            title = json.getString("title");
            shareUrl = json.getString("url");
            icon = json.getString("icon");
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void doShareAction(View view) {
        int index = Integer.valueOf((String)view.getTag()) - 1;

        // 微信或朋友圈
        if (index == 2 || index == 3) {
            if (!Utils.App.isWxAppSupported(this)) {
                Utils.UI.showToastShort("请手动打开微信客户端后再分享");
                return;
            }
        }

        Platform.ShareParams shareParams = shareParamsList.get(index);
        shareParams.setText(content);
        shareParams.setTitle(title);
        shareParams.setSite(title);
        shareParams.setSiteUrl(shareUrl);
        shareParams.setImageUrl(shareUrl);
        shareParams.setUrl(shareUrl);
        shareParams.setTitleUrl(shareUrl);
        shareParams.setShareType(Platform.SHARE_WEBPAGE);

        Platform platform = ShareSDK.getPlatform(shareNames[index]);


        platform.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                Utils.UI.showToastShort("分享成功");
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                Utils.UI.showToastShort("分享失败");
            }

            @Override
            public void onCancel(Platform platform, int i) {
                Utils.UI.showToastShort("取消分享");
            }
        });
        platform.share(shareParams);
    }

    private BridgeHandler shareHandler = new BridgeHandler() {
        @Override
        public void handler(String data, CallBackFunction function) {
            setShareParams(data);
        }
    };

    public void updateToolarbarTitle(String title) {
        mToolbarTitle.setText(title);
    }

    @NonNull
    @Override
    public MvpPresenter createPresenter() {
        return new MvpPresenter() {
            @Override
            public void attachView(MvpView view) {
            }
            @Override
            public void detachView(boolean retainInstance) {
            }
        };
    }
}
